package cn.com.cabinet.manager.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.base.util.PageUtils;

import com.cabinet.easy.util.Page;

import com.cabinet.web.entity.Seller;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.villagetravel.ssm.sendemail.MailAuthenticator;

import cn.com.cabinet.service.SellerServiceImpl;
import cn.com.easy.dto.MessageDTO;
import cn.com.easy.dto.PageRequestParamDTO;
import cn.com.easy.utils.FastJSONUtils;
import cn.com.easy.utils.MailUtils;
import cn.com.easy.utils.ResponseOutputUtils;
import cn.com.easy.utils.SecurityUtils;

@Controller
@RequestMapping("/manager/application")
public class SellerApplicationController {
	
	@Autowired
	private SellerServiceImpl sellerServiceImpl;
	
	@RequestMapping()
	public String redirectApplication(){
		return "/application/index";
		
	}
	
	@RequestMapping("/list")
	public void getSellerApplication(HttpServletRequest req, HttpServletResponse res, PageRequestParamDTO paramDto, Integer key, Integer status){
		System.out.println(status);
		
		try {
			int thispage = (paramDto.getPageNumber()-1)*10;
			
			List<Seller> cabinetList = sellerServiceImpl.getSellerApplication(status, thispage, paramDto.getPageSize());
			int totalcount = sellerServiceImpl.getSellerApplicationCount(status);
			Paginator p = new Paginator(thispage, paramDto.getPageSize(), totalcount);
			PageList<Seller> pageObject = new PageList<>(cabinetList, p);
			Page<Seller> page = PageUtils.getPage(pageObject);
			
			ResponseOutputUtils.renderJson(res, MessageDTO.newInstance("", true, page));
		} catch (Exception ex) {
			//logger.error("会员管理列表错误：", ex);
			ResponseOutputUtils.renderText(res, FastJSONUtils.toJsonString(MessageDTO.newInstance("", false, "会员管理列表错误")));
		}
	}

	@RequestMapping("/getone")
	@ResponseBody
	public Seller getSellerApplicationById(Long id){
		return sellerServiceImpl.getSellerApplicationById(id);
		
	}
	@RequestMapping("/update")
	@ResponseBody
	public String updateSellerApplication(Long id,String sMark){
		System.out.println("----------"+id);
		System.out.println("----------"+sMark);
		if(sMark == "1" || sMark.equals("1")){
			Seller s = sellerServiceImpl.getSellerApplicationById(id);
			s.setsMark(1);
			//为商家生成后台登入账号
			s.setsAccount(s.getsPhone());
			String password = SecurityUtils.SHA256("123456");
			s.setsPwd(password);
			//将账号通过邮箱发送给用户
			String context = "亲爱的："+s.getsTrueName()+"(先生/女士)您的加盟申请已通过我们的审核。\n您的账号为："+s.getsAccount()+"    密码：123456"+
							"   打开以下网站即可登录后台管理系统，并修改您的初始密码！\n"+
					        "http://s.yoyorv.cn";
			try {
				MailAuthenticator.send_email(s.getsEmail(), "智能柜商家加盟审核(系统邮件，无需回复)", context);
				MailUtils.sendText(s.getsEmail(), "智能柜商家加盟审核(系统邮件，无需回复)", context);
			} catch (Exception e) {
				System.out.println(e);
			}
			sellerServiceImpl.saueOrUpdateSellerApplicationById(s);
			return "审核通过";
		}else{
			if(sMark == "2" || sMark.equals("2")){
				Seller s = sellerServiceImpl.getSellerApplicationById(id);
				s.setsMark(2);
				//为商家生成后台登入账号
				//s.setsAccount(s.getsPhone());
				//s.setsPwd("123456");
				//将账号通过邮箱发送给用户
				String context = "亲爱的："+s.getsTrueName()+"(先生/女士)您的加盟申请未能通过我们的审核。\n"
								;
				try {
					MailAuthenticator.send_email(s.getsEmail(), "智能柜商家加盟审核(系统邮件，无需回复)", context);
					//MailUtils.sendText(s.getsEmail(), "智能柜商家加盟审核(系统邮件，无需回复)", context);
				} catch (Exception e) {
					System.out.println(e);
				}
				sellerServiceImpl.saueOrUpdateSellerApplicationById(s);
			}
			return "驳回审核";
		}
	
		
	} 
	
}
